package com.fl.nj.service.impl;

import com.fl.nj.entity.DatabaseConfig;
import com.fl.nj.mapper.DatabaseConfigMapper;
import com.fl.nj.service.DatabaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据库配置Service实现类
 * 实现数据库配置的业务逻辑
 */
@Service
public class DatabaseConfigServiceImpl implements DatabaseConfigService {
    @Autowired
    private DatabaseConfigMapper databaseConfigMapper;

    @Override
    @Transactional
    public void save(DatabaseConfig config) {
        // 验证配置名称是否已存在
        DatabaseConfig existingConfig = databaseConfigMapper.getByConfigName(config.getConfigName());
        if (existingConfig != null) {
            throw new RuntimeException("配置名称已存在: " + config.getConfigName());
        }
        
        // 验证必填字段
        validateConfig(config);
        
        // 保存配置
        databaseConfigMapper.save(config);
    }

    @Override
    @Transactional
    public void update(DatabaseConfig config) {
        // 验证配置是否存在
        DatabaseConfig existingConfig = databaseConfigMapper.getById(config.getId());
        if (existingConfig == null) {
            throw new RuntimeException("配置不存在");
        }
        
        // 验证配置名称是否已被其他配置使用
        DatabaseConfig configWithSameName = databaseConfigMapper.getByConfigName(config.getConfigName());
        if (configWithSameName != null && !configWithSameName.getId().equals(config.getId())) {
            throw new RuntimeException("配置名称已存在: " + config.getConfigName());
        }
        
        // 验证必填字段
        validateConfig(config);
        
        // 更新配置
        databaseConfigMapper.update(config);
    }

    @Override
    public DatabaseConfig getById(Long id) {
        return databaseConfigMapper.getById(id);
    }

    @Override
    public DatabaseConfig getByConfigName(String configName) {
        return databaseConfigMapper.getByConfigName(configName);
    }

    @Override
    public List<DatabaseConfig> getAll() {
        return databaseConfigMapper.getAll();
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 验证配置是否存在
        DatabaseConfig config = databaseConfigMapper.getById(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }
        
        // 删除配置
        databaseConfigMapper.delete(id);
    }

    /**
     * 验证数据库配置的必填字段
     * @param config 数据库配置对象
     */
    private void validateConfig(DatabaseConfig config) {
        if (config.getConfigName() == null || config.getConfigName().trim().isEmpty()) {
            throw new RuntimeException("配置名称不能为空");
        }
        if (config.getDbType() == null || config.getDbType().trim().isEmpty()) {
            throw new RuntimeException("数据库类型不能为空");
        }
        if (config.getUrl() == null || config.getUrl().trim().isEmpty()) {
            throw new RuntimeException("数据库URL不能为空");
        }
        if (config.getUsername() == null || config.getUsername().trim().isEmpty()) {
            throw new RuntimeException("用户名不能为空");
        }
        if (config.getPassword() == null || config.getPassword().trim().isEmpty()) {
            throw new RuntimeException("密码不能为空");
        }
        if (config.getDriverClassName() == null || config.getDriverClassName().trim().isEmpty()) {
            throw new RuntimeException("驱动类名不能为空");
        }
        
        // 验证数值字段
        if (config.getMaximumPoolSize() <= 0) {
            throw new RuntimeException("最大连接数必须大于0");
        }
        if (config.getMinimumIdle() < 0) {
            throw new RuntimeException("最小空闲连接数不能小于0");
        }
        if (config.getConnectionTimeout() <= 0) {
            throw new RuntimeException("连接超时时间必须大于0");
        }
        if (config.getIdleTimeout() <= 0) {
            throw new RuntimeException("空闲超时时间必须大于0");
        }
        if (config.getMaxLifetime() <= 0) {
            throw new RuntimeException("最大生命周期必须大于0");
        }
    }
}